#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using ZO.SmartCore.Windows.Forms.Design;

#endregion

namespace ZO.SmartCore.Windows.Forms
{
	/// <summary>
	/// Represents a Line Control.
	/// </summary>
	[ToolboxBitmap(typeof(SmartLine), "SmartCore.Windows.Forms.SmartLine.bmp")]
	[ToolboxItem(true)]
    [Designer(typeof(SmartLineDesigner))]
	public class SmartLine : SmartControl
	{

		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="SmartLine"/> class.
		/// </summary>
		public SmartLine()
		{
			InitializeComponent();

				// Avoid receiving the focus.
				SetStyle(ControlStyles.Selectable, false);

		}
		#endregion

		#region Destructor

		#endregion

		#region Fields
		private Color _DarkColor = SystemColors.ControlDark;
		private Color _LightColor = SystemColors.ControlLightLight;
	
		private LineStyle _Style = LineStyle.Separator;

		private Orientation _Orientation = Orientation.Horizontal;
		private Orientation previousOrientation = Orientation.Horizontal;


		#endregion

		#region Events

		#endregion

		#region Operators

		#endregion

		#region Properties

	


		/// <summary>
		/// Gets or sets the dark color of the control.
		/// </summary>
		/// <value>dark color of the control.</value>
		[Category("Appearance")]
		[RefreshPropertiesAttribute(RefreshProperties.Repaint)]
		public Color DarkColor
		{

			get { return _DarkColor; }

			set
			{
				_DarkColor = value;
				this.Invalidate();
			}
		}

		/// <summary>
		/// Gets or sets the light color of the control.
		/// </summary>
		/// <value>light color of the control.</value>
		[Category("Appearance")]
		[RefreshPropertiesAttribute(RefreshProperties.Repaint)]
		public Color LightColor
		{
			get { return _LightColor; }

			set
			{
				_LightColor = value;
				this.Invalidate();
			}
		}

		/// <summary>
		/// Gets or sets the style.
		/// </summary>
		/// <value>The style.</value>
		[Category("Appearance")]
		[RefreshPropertiesAttribute(RefreshProperties.Repaint)]
		public LineStyle Style
		{
			get
			{
				return _Style;
			}
			set
			{
				_Style = value;
				this.Invalidate();
			}
		}

		/// <summary>
		/// Gets or sets the orientation.
		/// </summary>
		/// <value>The orientation.</value>
		[Category("Appearance")]
		[RefreshPropertiesAttribute(RefreshProperties.All)]
		public Orientation Orientation
		{
			get
			{
				return _Orientation;
			}
			set
			{
				if (this.Orientation != value)
				{
					_Orientation = value;
					Refresh();
				}
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="SmartControl"/> is transparent.
		/// </summary>
		/// <value><c>true</c> if transparent; otherwise, <c>false</c>.</value>
		[Browsable(false)]
		public override bool Transparent
		{
			get { return base.Transparent; }
			set
			{
			}
		}
		#endregion

		#region Methods
		/// <summary>
		/// Raises the <see cref="Control.Resize"/> event.
		/// </summary>
		/// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		protected override void OnResize(EventArgs e)
		{
			this.Invalidate();
			base.OnResize(e);
		}

		/// <summary>
		/// Resizes the this.
		/// </summary>
		private void ResizeThis()
		{
			if (this.Style == LineStyle.Separator)
			{
				switch (this.Orientation)
				{
					case Orientation.Horizontal:

						if (previousOrientation == Orientation.Vertical)
						{
							if (this.Height != 2)
							{
								this.Width = this.Height;
								this.Height = 2;
							}
							previousOrientation = Orientation.Horizontal;
						}
						else
						{
							this.Height = 2;
						}
						break;
					case Orientation.Vertical:

						if (previousOrientation == Orientation.Horizontal)
						{

							if (this.Width != 2)
							{
								this.Height = this.Width;

								this.Width = 2;
							}
							previousOrientation = Orientation.Vertical;

						}
						else
						{
							this.Width = 2;
						}

						break;

					default:
						if (previousOrientation == Orientation.Vertical)
						{
							if (this.Height != 2)
							{
								this.Width = this.Height;
								this.Height = 2;
							}
							previousOrientation = Orientation.Horizontal;
						}
						else
						{
							this.Height = 2;
						}

						break;
				}

			}
		}


		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.SuspendLayout();
			// 
			// Line
			// 
			this.Name = "SmartLine";
			this.Size = new Size(50, 2);
			this.ResumeLayout(false);

		}


		/// <summary>
		/// Raises the <see cref="E:System.Windows.Forms.Control.Paint"></see> event.
		/// </summary>
		/// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"></see> that contains the event data.</param>
		protected override void OnPaint(PaintEventArgs e)
		{
			ResizeThis();

			if (this.Style == LineStyle.Separator)
			{
				Brush lightBrush = new SolidBrush(SystemColors.ControlLightLight);
				Brush darkBrush = new SolidBrush(SystemColors.ControlDark);

				Pen lightPen;
				Pen darkPen;

				switch (this.Orientation)
				{
					case Orientation.Horizontal:
						lightPen = new Pen(lightBrush, 1);
						darkPen = new Pen(darkBrush, 1);

						e.Graphics.DrawLine(darkPen, 0, 0, this.Width, 0);
						e.Graphics.DrawLine(lightPen, 0, 1, this.Width, 1);
						break;

					case Orientation.Vertical:
						lightPen = new Pen(lightBrush, 1);
						darkPen = new Pen(darkBrush, 1);

						e.Graphics.DrawLine(darkPen, 0, 0, 0, this.Height);
						e.Graphics.DrawLine(lightPen, 1, 0, 1, this.Height);
						break;
				}
			}
			else if (this.Style == LineStyle.Gradient)
			{

				Graphics g = e.Graphics;

				g.SmoothingMode = SmoothingMode.HighQuality;


				Rectangle rect = this.ClientRectangle;

				LinearGradientMode mode;

				if (this.Orientation == Orientation.Horizontal)
				{
					mode = LinearGradientMode.Horizontal;
				}
				else
				{
					mode = LinearGradientMode.Vertical;
				}

	


				LinearGradientBrush lb = new LinearGradientBrush(rect, this.LightColor,
					this.DarkColor, mode);

				g.FillRectangle(lb, rect);
	
			}


		
		}



		#endregion
	}
}
